package com.party.notify.template.service.impl;

import com.google.common.base.Strings;
import com.party.common.utils.StringUtils;
import com.party.core.model.system.SysConfig;
import com.party.core.service.system.ISysConfigService;
import com.party.notify.template.service.ITemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by wei.li
 *
 * @date 2017/4/7 0007
 * @time 10:30
 */

@Service
public class TemplateService implements ITemplateService {


    @Value("${mobile.domain}")
    private String domain;

    private static final String SMS_SIGN = "smsSign";

    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 替换模板
     * @param template 模板
     * @param map 内容
     * @return 内容
     */
    public String replace(String template, HashMap<String, Object> map) {
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String val = entry.getValue()==null? "":entry.getValue().toString();
            template = StringUtils.replace(template, entry.getKey(), val);
        }
        return template;
    }


    /**
     * 替换url
     * @param url
     * @param map
     * @return
     */
    public String replaceUrl(String url, HashMap<String, Object> map){
        url = this.replace(url, map);
        return domain + url;
    }

    /**
     * 短信签名
     * @param message 短信信息
     * @param partnerName 合作商名称
     * @return 短信内容（签名后）
     */
    @Override
    public String smsSign(String message, String partnerName) {
        StringBuilder stringBuilder = new StringBuilder(message);
        if (!Strings.isNullOrEmpty(partnerName)){
            stringBuilder.append("【").append(partnerName).append("】").append("✦");
        }
        SysConfig sysConfig = sysConfigService.findByCode(SMS_SIGN);
        stringBuilder.append(sysConfig.getValue());
        return stringBuilder.toString();
    }
}
